package com.be.beadmin.system.cronJob;

import com.be.beadmin.system.service.AuthorizeService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class ProjectInfoJob {
    @Resource
    private AuthorizeService authorizeService;

    public static String CLIENT_ID = "Corner";

    public static String CLIENT_SECRET = "6e4d504ded3243eda30c71473dc4d3d5";

    public static String USER_CODE = "09030020";

    public static String ENT_CODE = "001";

    public static String ORG_CODE = "01";

    public static String PROJECT_INFO_SQL_STRING = "SELECT c.name CustomerName, a.Customer CustomerCode, a.CustomerSite, " +
            "a.Linkman CustomerLinkman, a.SrcSODocNo SO, a.SrcSOLineNo SOitem, " +
            "a.DescFlexField_PrivateDescSeg1 SOOperatorCode, b.Name ProjectName, " +
            "a.Code ProjectCode, a.ProjectManagerName ProjectManager, " +
            "a.DescFlexField_PrivateDescSeg2 CabinetQTY, a.EndDate DeliveryDate, a.Org OrgCode " +
            "FROM CBO_Project a LEFT JOIN CBO_Project_Trl b ON b.ID = a.ID LEFT JOIN CBO_Customer_Trl c ON c.ID = a.Customer " +
            "WHERE a.Org = '1002201130000355' AND CreatedOn > DATEADD( HOUR, - 24, GETDATE( ))- 100;";

    public static String PRODUCT_ORDER_INFO_SQL_STRING = "SELECT SrcDoc_SrcDocNo SO,SrcDoc_SrcDocLineNo SOItem," +
            "b.code ProjectCode,DocNO MO,c.code Material,ProductQty QTY,a.Org OrgCode FROM MO_MO a LEFT JOIN CBO_Project b " +
            "ON b.ID = a.Project LEFT JOIN CBO_ItemMaster c ON c.ID = a.ItemMaster WHERE a.Org = '1002201130000355' AND " +
            "a.CreatedOn > DATEADD( HOUR, - 24,GETDATE( ) ) - 100;";

    public static String MATERIAL_BASIC_INFO_SQL_STRING = "SELECT Code MaterialCode,a.Name MaterialName,SPECs MaterialSpec,-- Description Para,\n" +
            "b.name Unit,ProductionCategory Category,DescFlexField_PrivateDescSeg4 BarCode,DescFlexField_PrivateDescSeg1 Brand," +
            "Effective_IsEffective Effective,DescFlexField_PrivateDescSeg3 MaterialProperty FROM CBO_ItemMaster a LEFT JOIN base_uom_trl b " +
            "ON b.ID = a.InventoryUOM WHERE a.Org = '1002201130000355'  AND  a.CreatedOn >  DATEADD( HOUR, - 24,  GETDATE( ) ) - 100;";

    public static String PUCHARSE_ORDER_INFO_SQL_STRING = "SELECT SrcDoc_SrcDocNo PO,SrcDoc_SrcDocLineNo POItem," +
            "ItemInfo_ItemCode MaterialCode,ItemInfo_ItemName MaterialName,-- Brand PlanQtyCU PlanQTY,\n" +
            "QualifiedQtyCU ReceivedQTY,PlanArrivedDate,ConfirmDate ActualArrivedDate,PurOper Purchaser,b.Code " +
            "ProjectCode FROM PM_RcvLine a LEFT JOIN CBO_Project b ON b.ID = a.Project WHERE  a.CreatedOn >  DATEADD( HOUR, - 24,  GETDATE( ) )- 100";
    @Scheduled(cron = "0 0 0 * * ?")    //每天晚上12点执行
    public void scheduledTask() throws Exception {

        getMaterialBasicInfo(CLIENT_ID, CLIENT_SECRET, USER_CODE, ENT_CODE, ORG_CODE, MATERIAL_BASIC_INFO_SQL_STRING);

        getProjectInfo(CLIENT_ID, CLIENT_SECRET, USER_CODE, ENT_CODE, ORG_CODE, PROJECT_INFO_SQL_STRING);

        getProductOrderInfo(CLIENT_ID, CLIENT_SECRET, USER_CODE, ENT_CODE, ORG_CODE, PRODUCT_ORDER_INFO_SQL_STRING);

        getPurchaseOrderInfo(CLIENT_ID, CLIENT_SECRET, USER_CODE, ENT_CODE, ORG_CODE, PUCHARSE_ORDER_INFO_SQL_STRING);
    }

    /**
     * 项目立项信息查询
     * @param clientid
     * @param clientsecret
     * @param userCode
     * @param entcode
     * @param orgcode
     * @param sqlString
     * @throws Exception
     */
    public void getProjectInfo(String clientid, String clientsecret,
                               String userCode, String entcode, String orgcode,
                               String sqlString) throws Exception {
        if (clientid == null) {
            clientid = CLIENT_ID;
        }
        if (clientsecret == null) {
            clientsecret = CLIENT_SECRET;
        }
        if (userCode == null) {
            userCode = USER_CODE;
        }
        if (entcode == null) {
            entcode = ENT_CODE;
        }
        if (orgcode == null) {
            orgcode = ORG_CODE;
        }
        if (sqlString == null) {
            sqlString = PROJECT_INFO_SQL_STRING;
        }
        boolean is_request = authorizeService.checkTime(new Date(System.currentTimeMillis()));
        //超时3分钟
        if(is_request){
            String data = authorizeService.getAuthorizeCode(clientid, clientsecret);
            authorizeService.getLoginCode(userCode, entcode, orgcode, data);
        }
        authorizeService.getProjectInfo(authorizeService.getTOKEN(), sqlString);
    }

    /**
     * 生产订单信息查询
     * @param clientid
     * @param clientsecret
     * @param userCode
     * @param entcode
     * @param orgcode
     * @param sqlString
     * @throws Exception
     */
    public void getProductOrderInfo(String clientid, String clientsecret,
                               String userCode, String entcode, String orgcode,
                               String sqlString) throws Exception {
        if (clientid == null) {
            clientid = CLIENT_ID;
        }
        if (clientsecret == null) {
            clientsecret = CLIENT_SECRET;
        }
        if (userCode == null) {
            userCode = USER_CODE;
        }
        if (entcode == null) {
            entcode = ENT_CODE;
        }
        if (orgcode == null) {
            orgcode = ORG_CODE;
        }
        if (sqlString == null) {
            sqlString = PRODUCT_ORDER_INFO_SQL_STRING;
        }
        boolean is_request = authorizeService.checkTime(new Date(System.currentTimeMillis()));
        //超时3分钟
        if(is_request){
            String data = authorizeService.getAuthorizeCode(clientid, clientsecret);
            authorizeService.getLoginCode(userCode, entcode, orgcode, data);
        }
        authorizeService.getProductOrderInfo(authorizeService.getTOKEN(), sqlString);
    }

    /**
     * 物料基础信息查询
     * @param clientid
     * @param clientsecret
     * @param userCode
     * @param entcode
     * @param orgcode
     * @param sqlString
     */
    public void getMaterialBasicInfo(String clientid, String clientsecret,
                                    String userCode, String entcode, String orgcode,
                                    String sqlString) throws Exception {
        if (clientid == null) {
            clientid = CLIENT_ID;
        }
        if (clientsecret == null) {
            clientsecret = CLIENT_SECRET;
        }
        if (userCode == null) {
            userCode = USER_CODE;
        }
        if (entcode == null) {
            entcode = ENT_CODE;
        }
        if (orgcode == null) {
            orgcode = ORG_CODE;
        }
        if (sqlString == null) {
            sqlString = MATERIAL_BASIC_INFO_SQL_STRING;
        }
        boolean is_request = authorizeService.checkTime(new Date(System.currentTimeMillis()));
        //超时3分钟
        if(is_request){
            String data = authorizeService.getAuthorizeCode(clientid, clientsecret);
            authorizeService.getLoginCode(userCode, entcode, orgcode, data);
        }
        authorizeService.getMaterialBasicInfo(authorizeService.getTOKEN(), sqlString);
    }

    /**
     * 猜个屁订单信息查询
     * @param clientid
     * @param clientsecret
     * @param userCode
     * @param entcode
     * @param orgcode
     * @param sqlString
     * @throws Exception
     */
    public void getPurchaseOrderInfo(String clientid, String clientsecret,
                               String userCode, String entcode, String orgcode,
                               String sqlString) throws Exception {
        if (clientid == null) {
            clientid = CLIENT_ID;
        }
        if (clientsecret == null) {
            clientsecret = CLIENT_SECRET;
        }
        if (userCode == null) {
            userCode = USER_CODE;
        }
        if (entcode == null) {
            entcode = ENT_CODE;
        }
        if (orgcode == null) {
            orgcode = ORG_CODE;
        }
        if (sqlString == null) {
            sqlString = PUCHARSE_ORDER_INFO_SQL_STRING;
        }
        boolean is_request = authorizeService.checkTime(new Date(System.currentTimeMillis()));
        //超时3分钟
        if(is_request){
            String data = authorizeService.getAuthorizeCode(clientid, clientsecret);
            authorizeService.getLoginCode(userCode, entcode, orgcode, data);
        }
        authorizeService.getPurchaseOrderInfo(authorizeService.getTOKEN(), sqlString);
    }

}
